package com.czxy.changgou3.service;

import com.alibaba.fastjson.JSON;
import com.czxy.changgou3.cart.Cart;
import com.czxy.changgou3.cart.CartItem;
import com.czxy.changgou3.feign.SkuFeign;
import com.czxy.changgou3.mapper.AddressMapper;
import com.czxy.changgou3.mapper.OrderGoodsMapper;
import com.czxy.changgou3.mapper.OrderMapper;
import com.czxy.changgou3.pojo.Address;
import com.czxy.changgou3.pojo.Order;
import com.czxy.changgou3.pojo.OrderGoods;
import com.czxy.changgou3.pojo.User;
import com.czxy.changgou3.vo.OrderVo;
import com.czxy.utils.IdWorker;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.Iterator;
import java.util.Map;

/**
 * @author zhangmengxiang1@itcast.cn
 * @version 1.0
 * @date 2020/5/5
 */
@Service
@Transactional
public class OrderService {

    @Resource
    private IdWorker idWorker;

    @Resource
    private AddressMapper addressMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private OrderGoodsMapper orderGoodsMapper;

    @Resource
    private SkuFeign skuFeign;

    public Long createOrder(User user , OrderVo orderVo) {
        //生成订单
        Order order = new Order();
        //生成订单号
        //生成orderId
        long sn = idWorker.nextId();
        order.setSn(sn);

        //设置用户信息
        order.setUserId(user.getId());

        //设置地址信息
        //获得前台传输过来的收货地址和收货人信息，生成订单，保存到数据库
        Address address = addressMapper.selectByPrimaryKey(orderVo.getAddressId());
        order.setShrName(address.getShrName());
        order.setShrMobile(address.getShrMobile());
        order.setShrProvince(address.getShrProvince());
        order.setShrCity(address.getShrCity());
        order.setShrArea(address.getShrArea());
        order.setShrAddress(address.getShrAddress());

        //设置状态，创建订单的时候，默认情况下未支付状态
        order.setStatus(0);
        order.setCreatedAt(new Date());

        //获得购物车 从redis 获取当前用户对应的购物车
        String key = "cart" + user.getId();
        String cartJsonStr = stringRedisTemplate.opsForValue().get(key);
        Cart cart = JSON.parseObject(cartJsonStr, Cart.class);
        System.out.println(cart);


        //保存购物车中已经勾选的商品信息
        //便利购物想
        Iterator<CartItem> it = cart.getData().values().iterator();
        double total = 0;
        while (it.hasNext()){
            CartItem cartItem = it.next();
            if (cartItem.getChecked()){
                //将购物车中的商品赋值给OrderGoods
                OrderGoods orderGoods = new OrderGoods();
                orderGoods.setSn(idWorker.nextId());
                orderGoods.setSkuId(cartItem.getSkuid());
                orderGoods.setSpuId(cartItem.getSpuid());
                orderGoods.setNumber(cartItem.getCount());
                //{"id_list":"1:1|2:6|6:22","id_txt":"{\"机身颜色\":\"白色\",\"内存\":\"3GB\",\"机身存储\":\"16GB\"}"}

                orderGoods.setSpecList(JSON.toJSONString(cartItem.getSpecInfoIdTxt()));
                orderGoods.setSkuName(cartItem.getGoodsName());
                orderGoods.setLogo(cartItem.getMidlogo());
                orderGoods.setPrice(cartItem.getPrice());
                // 3.3 保存购物车中的商品信息
                orderGoodsMapper.insert(orderGoods);
                // 3.5  远程调用方法，将该商品的数量减少
                skuFeign.updateSkuNum(cartItem.getSkuid(),cartItem.getCount());
                // 3.4 购物车中移除该商品
                it.remove();
                total +=  (cartItem.getCount() * cartItem.getPrice());
            }
        }

        //设置总价格
        order.setTotalPrice(total);
        //保存订单
        orderMapper.insert(order);

        //3.6 更新redis购物车
        stringRedisTemplate.opsForValue().set(key , JSON.toJSONString(cart));
        //4 返回sn
        return sn;

    }

}
